import axios from "axios";
//import store from "../store";
// import Router from "vue-router"
import {whiteList} from "./white";
import router from "../router/index"
import Cookies from 'js-cookie'

import Message from "ant-design-vue/lib/message"
import {notification} from 'ant-design-vue'

const request = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    //使用认证 cookie
    withCredentials: true,
    timeout: 15000,
});
axios.defaults.withCredentials = true

request.interceptors.request.use(
    config => {
        console.log(config)
        if (whiteList.includes(config.url) || whiteList.find(uri => config.url.startsWith(uri))) {
            return config;
        }
        let mmUserTokenWeb = sessionStorage.getItem("mm-user-token-web");
        if (mmUserTokenWeb) {
            //***如果不存在就不添加该请求头 否则如果请求的是白名单接口 还有Authorization头的话 则Oauth2还是会进行验证码
            config.headers.common = {
                'Authorization': 'Bearer ' + sessionStorage.getItem("mm-user-token-web")
            }
        }
        //请求拦截
        return config;
    },
    error => {
        //出现异常
        return Promise.reject(error);
    }
);
request.interceptors.response.use(
    response => {

        //响应拦截，其他页面无需考虑处理失败的false情况
        const respData = response.data;

        if (respData.flag === false) {
            console.log("拦截--> error-->")
            console.log(response)
            if (respData.code === 5006) {
                Cookies.remove("user_token")
                router.replace({
                    path: '/login',
                    query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面
                })
            }
            if (respData.message) {
                Message.warning(respData.message)
            } else {
                Message.warning("请稍后再试")
            }
        }

        return response;
    },
    error => {
        /*Message({
          message: error.message,
          type: "error",
          duration: 5 * 1000
        });*/

        console.log(error.response.data)
        if (error.response.status) {
            switch (error.response.status) {
                case 500:
                    if (error.response.data.message === "GENERAL") {
                        Message.warning("请稍后再试", 1)
                    } else {
                        router.routes.push("/500")
                    }
                    break;
                case 404:
                    Message.warning("很抱歉，资源未找到！", 2)

                    break;
                case 403:
                    Message.warning("拒绝访问", 2)

                    /* store.commit("SET_TOKEN", "");
                     store.commit("SET_ROLE", "");
                     store.commit("SET_USER", "");*/

                    router.replace({
                        path: '/login',
                        query: {
                            redirect: this.$router.currentRoute.fullPath
                        }
                    })

                    break;
                case 401:
                    Message.warning("未认证", 2)
                    break;
                case 504:
                    notification.error({message: '系统提示', description: '网络超时'})
                    break

                default:
                    Message.error("参数错误", 2)
                // Message({
                //   message:error.response.message,
                //   duration:2000,
                // })
                //  Router.push("/error")
            }

        }

        return Promise.reject(error);
    }
)
;

export default request;
